package org.aurora.common.serialization;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Byte2ObjectSerializationFactory {
    private static final Map<SerializationType, Byte2ObjectSerialization> SERIALIZATION_MAP = new ConcurrentHashMap<>();

    static {
        init();
    }

    private static void init() {
        SERIALIZATION_MAP.put(SerializationType.JSON_UTF8, new FastjsonUtf8Byte2ObjectSerialization());
    }

    public static byte[] serialization(Object o, SerializationType serializationType) {
        return getSerialization(serializationType).serialization(o);
    }

    public static <T> T deserialization(byte [] bytes, Class<T> clazz, SerializationType serializationType) {
        return getSerialization(serializationType).deserialization(bytes, clazz);
    }

    private static Byte2ObjectSerialization getSerialization(SerializationType serializationType) {
        Byte2ObjectSerialization byte2ObjectSerialization = SERIALIZATION_MAP.get(serializationType);

        if (byte2ObjectSerialization == null) {
            throw new RuntimeException("serialization not exist");
        }
        return byte2ObjectSerialization;
    }
}
